package com.ruoyi.worklog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.worklog.domain.WlProject;
import com.ruoyi.worklog.service.ProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 项目Controller
 *
 * @author ruoyi
 * @date 2022-07-25
 */
@RestController
@RequestMapping("/worklog/project")
public class projectController extends BaseController
{
    @Autowired
    private ProjectService projectService;

    /**
     * 查询项目列表
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(WlProject wlProject)
    {
        startPage();
        List<WlProject> list = projectService.selectWlProjectList(wlProject);
        return getDataTable(list);
    }

    /**
     * 查询某位leader的项目列表
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:listbyid')")
    @GetMapping("/listbyid/{leader_id}")
    public TableDataInfo leaderList(@PathVariable Long leader_id)
    {
        startPage();
        List<WlProject> list = projectService.selectWlProjectListByLeaderId(leader_id);
        return getDataTable(list);
    }

    /**
     * 获取项目详细信息
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(projectService.selectWlProjectById(id));
    }

    /**
     * 新增项目
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:add')")
//    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor=Exception.class)
    public AjaxResult add(@Validated @RequestBody WlProject wlProject)
    {
        // 新增的项目，和已有的项目名称不能相同
        if (projectService.checkProjectNameUnique(wlProject))
        {
            return AjaxResult.error("新增项目:" + wlProject.getProject_name() + "'失败，项目名称已存在");
        }
        return toAjax(projectService.insertWlProject(wlProject));
    }

    /**
     * 修改项目
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:edit')")
//    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WlProject wlProject)
    {

        return toAjax(projectService.updateWlProject(wlProject));
    }

    /**
     * 删除项目
     */
//    @PreAuthorize("@ss.hasPermi('worklog:project:remove')")
//    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor=Exception.class)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectService.deleteWlProjectByIds(ids));
    }
}
